﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calender
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建日历表

            int[,] calender = new int[6, 7];
            //初始化第一行
            string[] week = { "一", "二", "三", "四", "五", "六", "日" };
            Console.WriteLine("这个月的1号是星期几？");
            int first = Convert.ToInt32(Console.ReadLine());
            int today = DateTime.Now.Day;
            
            int date = 1;
            Console.WriteLine("这个月有几天？");
            int last=Convert.ToInt32(Console.ReadLine());
            
            int j = first - 1;
           
            
            //确定这个月的日期
            int w = 0;
            for(;w<6;w++)
            {
                //遍历每周
                for(;j<7;j++)
                {
                    calender[w, j] = date;
                    date++;
                    if (date > last)
                        break;
                    
                }
                if (date > last)
                    break;
                //初始化j
                j = 0;

            }
            //确定上个月的日期
            Console.WriteLine("上个月有几天？");
            int lastday = Convert.ToInt32(Console.ReadLine());
            for (int k = first - 2; k >= 0; k--)
            {
                
                calender[0, k] = lastday;
                lastday--;
            }
            //确定下个月的日期
            int nextday = 1;
            int nextmon = j + 1;
            while(nextmon<7)
            {
                calender[w, nextmon] = nextday;
                nextday++;
                nextmon++;
            }
            if(nextmon==7)
            {
                nextmon = 0;
                while (nextmon < 7)
                {
                    calender[w+1, nextmon] = nextday;
                    nextday++;
                    nextmon++;
                }
            }

            //输出日历
            int thisyear = DateTime.Now.Year;
            int thismonth = DateTime.Now.Month;
            Console.Write(new string(' ', 10));
            Console.WriteLine(thisyear + "年" + thismonth + "月");
            for (int i=0;i<week.Length;i++)
            {
                string str = week[i].PadLeft(4);
                Console.Write(str);
            }
            Console.WriteLine();
            for(int i=0;i<6;i++)
            {
                for(int k=0;k<7;k++)
                {
                    //用蓝色输出上个月的日期
                    if (i == 0 && k < first - 1)
                    {
                        Console.ForegroundColor = ConsoleColor.Blue;
                        string lastmon = Convert.ToString(calender[i, k]);
                        lastmon = lastmon.PadLeft(5);

                        Console.Write(lastmon);
                        Console.ForegroundColor = ConsoleColor.White;
                        continue;
                    }
                    //用蓝色输出下个月日期
                    if ((i == w && k > j)||i>w)
                    {
                        Console.ForegroundColor = ConsoleColor.Blue;
                        string next = Convert.ToString(calender[i, k]);
                        next = next.PadLeft(5);
                        Console.Write(next);

                        continue;
                    }
                    //输出这个月的日期  
                    string str = Convert.ToString(calender[i, k]);
                    str = str.PadLeft(5);
                    if (calender[i, k] == today)
                        Console.BackgroundColor = ConsoleColor.Blue;
                    else
                        Console.BackgroundColor = ConsoleColor.Black;
                    
                    Console.Write(str);
                }
                Console.WriteLine();
            }
            
            Console.ReadKey();
        }
    }
}
